<template>
	<view class="container">
		<view class="header" v-if="isLogin">
			<image class="headicon" @click="changeHeadIcon" :src="'http://localhost:8090/foodapp/'+userinfo.icon">
			</image>
			
		</view>
		<view class="">
			<text class="username" v-if="isLogin">欢迎回来,{{ userinfo.username }}</text>
		</view>
		<view class="header" v-if="!isLogin">
			<image class="headicon" src="">
			</image>
			<text class="username" v-if="!isLogin">请退出、登录</text>
		</view>
		
		<view class="entry-wrapper">
			<view class="item" @click="toInfoPage">
				<text>个人信息</text>
				<image src="../../static/more.png" mode=""></image>
			</view>
			<view class="item" @click="gofabiao">
				<text>发表评价</text>
				<image src="../../static/more.png" mode=""></image>
			</view>
			<view class="item" @click="gocommentlist">
				<text>评论列表</text>
				<image src="../../static/more.png" mode=""></image>
			</view>
		</view>
		<button class="button" type="primary":disabled="!isLogin" @click="logout">退出</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {
					id: '',
					telephone: '',
					password: '',
					icon: '',
					nickname:''
				},
				isLogin: false
			}

		},
		methods: {
			getUserInfo() {
				let userStr = uni.getStorageSync('userInfo')

				console.log(userStr);
				if (userStr) {
					let userinfo = JSON.parse(userStr)
					this.userinfo = userinfo
					this.isLogin = true
				} else {
					this.userinfo.username = null
					this.userinfo.nickname = null
					this.userinfo.icon = '../../static/my.png'
					this.isLogin = false
				}
			},
			toInfoPage() {
				uni.navigateTo({
					url: './xinxi?item=' + encodeURIComponent(JSON.stringify(this.userinfo))
				});
			},
			logout() {
				uni.setStorageSync('userInfo', null)
				this.getUserInfo()
				uni.navigateTo({
					url: '/pages/my/login'
				})
			},
			changeHeadIcon() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						let filePath = res.tempFilePaths[0]
						uni.uploadFile({
							url: this.$baseUrl+'user/upload_headpic',
							filePath: filePath,
							name: 'file',
							formData: {
								id: 1
							},
							success: (uploadRes) => {
								console.log(uploadRes.data);
								let resdata = JSON.parse(uploadRes.data)
								console.log(resdata)
								this.userinfo.icon = resdata.data.icon
							}
						})
					}
				})
			},
			gofabiao() {
				uni.navigateTo({
					url: '/pages/my/fabiao'
				})
			},

			gocommentlist() {
				uni.navigateTo({
					url: '/pages/my/commentlist'
				})
			},
		},
		onShow() {
			this.getUserInfo();
		},
	}
</script>

<style>
	.header {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;


	}

	.headicon {
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
		margin: 100rpx;
	}

	.entry-wrapper {
		width: 100%;
		margin-top: 50rpx;
	}

	.entry-wrapper .item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		height: 80rpx;
		align-items: center;
		border-bottom: #333 solid 1rpx;
	}

	.entry-wrapper .item:first-child {
		border-top: #333 solid 1rpx;
	}

	.entry-wrapper .item image {
		width: 48rpx;
		height: 48rpx;
	}

	.button {	
		margin: 10px;
	}
</style>